package com.example.leero.graduation.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.LogUtil;
import com.example.leero.graduation.adapter.ScreenGridViewAdapter;
import com.example.leero.graduation.customView.MyGridView;
import com.example.leero.graduation.model.ActItem;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class SearchActivity extends AppCompatActivity {

    private MyGridView gv_screen_type, gv_screen_sex, gv_screen_time;
    private Button btn_screen_reset, btn_screen_submit;
    private ScreenGridViewAdapter screen_type_adapter, screen_sex_adapter, screen_time_adapter;
    private ImageButton back;
    private TextView title;

    private int sport_selection = 0, sex_selection = 0, time_selection = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();
        initScreenCotent();

        // 重置标签
        btn_screen_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                screen_type_adapter.setSelected(0);
                screen_type_adapter.notifyDataSetChanged();
                screen_sex_adapter.setSelected(0);
                screen_sex_adapter.notifyDataSetChanged();
                screen_time_adapter.setSelected(0);
                screen_time_adapter.notifyDataSetChanged();
            }
        });

        // 提交筛选条件
        btn_screen_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitSelection();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        gv_screen_type = (MyGridView) findViewById(R.id.gv_screen_type);
        gv_screen_sex = (MyGridView) findViewById(R.id.gv_screen_sex);
        gv_screen_time = (MyGridView) findViewById(R.id.gv_screen_time);
        btn_screen_reset = (Button) findViewById(R.id.btn_screen_reset);
        btn_screen_submit = (Button) findViewById(R.id.btn_screen_submit);
        back = (ImageButton) findViewById(R.id.iv_left);
        title = (TextView) findViewById(R.id.tv_title);

        title.setText("筛选活动");
    }

    /**
     * 筛选标签初始化
     */
    private void initScreenCotent() {
        String[] type = {"不限", "跑步", "羽毛球", "篮球", "足球", "网球", "乒乓球", "自行车"};
        String[] sex = {"不限", "男", "女"};
        String[] time = {"不限", "之前", "今天", "未来三天", "未来七天"};

        screen_type_adapter = new ScreenGridViewAdapter(this, type);
        gv_screen_type.setAdapter(screen_type_adapter);
        screen_sex_adapter = new ScreenGridViewAdapter(this, sex);
        gv_screen_sex.setAdapter(screen_sex_adapter);
        screen_time_adapter = new ScreenGridViewAdapter(this, time);
        gv_screen_time.setAdapter(screen_time_adapter);

        gv_screen_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sport_selection = position;
                screen_type_adapter.setSelected(position);
                screen_type_adapter.notifyDataSetChanged();
            }
        });
        gv_screen_sex.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sex_selection = position;
                screen_sex_adapter.setSelected(position);
                screen_sex_adapter.notifyDataSetChanged();
            }
        });
        gv_screen_time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                time_selection = position;
                screen_time_adapter.setSelected(position);
                screen_time_adapter.notifyDataSetChanged();
            }
        });
    }

    private void commitSelection() {
        OkHttpUtils
                .post()
                .url(API.SELECT_ACTIVITY)
                .addParams("sport", ""+sport_selection).addParams("sex", ""+sex_selection)
                .addParams("time", ""+time_selection).addParams("page", "1")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(SearchActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        List<ActItem> data = new ArrayList<>();
                        data.addAll(GsonUtil.jsonToArrayList(GsonUtil.getData(response), ActItem.class));

                        Intent intent = new Intent();
                        intent.putExtra("actItem", (Serializable) data);
                        intent.putExtra("isSearch", true);
                        SearchActivity.this.setResult(1, intent);
                    }

                    @Override
                    public void onAfter(int id) {
                        finish();
                    }
                });
    }


}
